package com.ruoyi.conference.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.IntStream;

import com.ruoyi.common.core.page.BooleanUtils;
import org.apache.commons.lang3.time.DateUtils;
import com.ruoyi.conference.domain.MeetingType;
import com.ruoyi.conference.mapper.MeetingTypeMapper;
import com.ruoyi.personnel.domain.Department;
import com.ruoyi.personnel.domain.Employee;
import com.ruoyi.personnel.mapper.DepartmentMapper;
import com.ruoyi.personnel.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.conference.mapper.MeetingRoomManageMapper;
import com.ruoyi.conference.domain.MeetingRoomManage;
import com.ruoyi.conference.service.IMeetingRoomManageService;

/**
 * 会议管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-04
 */
@Service
public class MeetingRoomManageServiceImpl implements IMeetingRoomManageService {
    @Autowired
    private MeetingRoomManageMapper meetingRoomManageMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private MeetingTypeMapper meetingTypeMapper;


    /**
     * 查询会议管理
     *
     * @param mrmId 会议管理主键
     * @return 会议管理
     */
    @Override
    public MeetingRoomManage selectMeetingRoomManageByMrmId(Integer mrmId) {

        return meetingRoomManageMapper.selectMeetingRoomManageByMrmId(mrmId);
    }

    /**
     * 查询会议记录列表
     *
     * @param meetingRoomManage 会议记录
     * @return 会议记录
     */
    @Override
    public List<MeetingRoomManage> selectMeetingRoomManageRecordList(MeetingRoomManage meetingRoomManage) {
        List<MeetingRoomManage> meetingRoomManageList = meetingRoomManageMapper.selectMeetingRoomManageRecordList(meetingRoomManage);
        for (MeetingRoomManage manage : meetingRoomManageList) {
            Department department = departmentMapper.selectDepartmentByDepId(manage.getDepId());
            Employee employee = employeeMapper.selectEmployeeByEmpId(manage.getEmpId());
            MeetingType meetingType = meetingTypeMapper.selectMeetingTypeByMtId(manage.getMtId());
            manage.setDepName(department.getDepName());
            manage.setEmpName(employee.getEmpName());
            manage.setEmpPhone(employee.getEmpPhone());
            manage.setMtType(meetingType.getMtType());
        }
        return meetingRoomManageList;
    }

    /**
     * 查询会议管理列表
     *
     * @param meetingRoomManage 会议管理
     * @return 会议管理
     */
    @Override
    public List<MeetingRoomManage> selectMeetingRoomManageList(MeetingRoomManage meetingRoomManage) {
        List<MeetingRoomManage> meetingRoomManageList = meetingRoomManageMapper.selectMeetingRoomManageList(meetingRoomManage);
        for (MeetingRoomManage manage : meetingRoomManageList) {
            Department department = departmentMapper.selectDepartmentByDepId(manage.getDepId());
            Employee employee = employeeMapper.selectEmployeeByEmpId(manage.getEmpId());
            MeetingType meetingType = meetingTypeMapper.selectMeetingTypeByMtId(manage.getMtId());
            manage.setDepName(department.getDepName());
            manage.setEmpName(employee.getEmpName());
            manage.setMtType(meetingType.getMtType());
        }
        return meetingRoomManageList;
    }

    /**
     * 新增会议管理
     *
     * @param meetingRoomManage 会议管理
     * @return 结果
     */
    @Override
    public BooleanUtils insertMeetingRoomManage(MeetingRoomManage meetingRoomManage) {
        Date queueDate = new Date();
        MeetingRoomManage meetingRoomManage1 = new MeetingRoomManage();
        meetingRoomManage1.setMrPlace(meetingRoomManage.getMrPlace());
//        meetingRoomManage1.setMrmId(meetingRoomManage.getMrmId());
        List<MeetingRoomManage> meetingRoomManages = meetingRoomManageMapper.selectMeetingRoomManageList(meetingRoomManage1);

        //对比时间
        //开始时间
        Date begintime = meetingRoomManage.getBegintime();
        long begintime_ms = begintime.getTime();
        //系统时间
        long queueDateTime = queueDate.getTime();
        //结束时间
        Date finishtime = meetingRoomManage.getFinishtime();
        long finishtime_ms = finishtime.getTime();
        String[] arr = new String[meetingRoomManages.size()];
        boolean am_i = DateUtils.isSameDay(begintime, finishtime);
        if (!am_i) {
            BooleanUtils booleanUtils = new BooleanUtils();
            booleanUtils.setFlag(false);
            booleanUtils.setMag("开始时间和结束时间必须是同一天!");
            return booleanUtils;
        } else if (begintime_ms > finishtime_ms) {
            BooleanUtils booleanUtils = new BooleanUtils();
            booleanUtils.setFlag(false);
            booleanUtils.setMag("会议开始时间不能小于结束时间!");
            return booleanUtils;
        } else if (begintime_ms < queueDateTime) {
            BooleanUtils booleanUtils = new BooleanUtils();
            booleanUtils.setFlag(false);
            booleanUtils.setMag("会议开始时间不能小于当前时间时间!");
            return booleanUtils;
        }

        for (int i = 0; i < arr.length; i++) {
            MeetingRoomManage manage = meetingRoomManages.get(i);

            Date oldBeginTime = manage.getBegintime();
            Date oldFinishTime = manage.getFinishtime();
            Date newBeginTime = meetingRoomManage.getBegintime();
            Date newFinishTime = meetingRoomManage.getFinishtime();

            long oldBeginDate = oldBeginTime.getTime();
            long oldEndDate = oldFinishTime.getTime();
            long newBeginDate = newBeginTime.getTime();
            long newEndDate = newFinishTime.getTime();

            if ((oldBeginDate < newBeginDate && oldEndDate < newBeginDate) || (newBeginDate < oldBeginDate && newEndDate < oldBeginDate)) {
                System.out.println("添加");
                arr[i] = "0";
            } else {
                System.out.println("会议时间冲突");
                arr[i] = "1";
            }
        }
        String str = Arrays.toString(arr);
        if (str.contains("1")) {
            BooleanUtils booleanUtils = new BooleanUtils();
            booleanUtils.setFlag(false);
            booleanUtils.setMag("会议时间冲突");
            return booleanUtils;
        } else {
            meetingRoomManage.setNewdatetime(queueDate);
            meetingRoomManage.setSubscribetime(queueDate);
            meetingRoomManage.setVersion(0);
            meetingRoomManageMapper.insertMeetingRoomManage(meetingRoomManage);
            BooleanUtils booleanUtils = new BooleanUtils();
            booleanUtils.setFlag(true);
            booleanUtils.setMag(null);
            return booleanUtils;
        }
    }

    /**
     * 修改会议管理
     *
     * @param meetingRoomManage 会议管理
     * @return 结果
     */
    @Override
    public BooleanUtils updateMeetingRoomManage(MeetingRoomManage meetingRoomManage) {
//        Date queueDate = new Date();
//        MeetingRoomManage meetingRoomManage1 = new MeetingRoomManage();
//        meetingRoomManage1.setMrPlace(meetingRoomManage.getMrPlace());
//        List<MeetingRoomManage> meetingRoomManages = meetingRoomManageMapper.selectMeetingRoomManageList(meetingRoomManage1);
//        for (int q = 0; q < meetingRoomManages.size(); q++) {
//            if (meetingRoomManages.get(q).getMrmId().equals(meetingRoomManage.getMrmId())){
//                meetingRoomManages.remove(q);
////                q--;
//            }
//        }
//        String[] arr = new String[meetingRoomManages.size()];
//        for (int i = 0; i < arr.length; i++) {
//            MeetingRoomManage manage = meetingRoomManages.get(i);
//
//            Date oldBeginTime = manage.getBegintime();
//            Date oldFinishTime = manage.getFinishtime();
//            Date newBeginTime = meetingRoomManage.getBegintime();
//            Date newFinishTime = meetingRoomManage.getFinishtime();
//
//            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            String oldBeginTimeOne = formatter.format(oldBeginTime);
//            String oldFinishTimeOne = formatter.format(oldFinishTime);
//            String newBeginTimeOne = formatter.format(newBeginTime);
//            String newFinishTimeOne = formatter.format(newFinishTime);

//            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            Date oldBeginDate = null;
//            Date oldEndDate = null;
//            Date newBeginDate = null;
//            Date newEndDate = null;
//            try {
//                oldBeginDate = df.parse(oldBeginTimeOne);
//                oldEndDate = df.parse(oldFinishTimeOne);
//                newBeginDate = df.parse(newBeginTimeOne);
//                newEndDate = df.parse(newFinishTimeOne);
//
//            } catch (ParseException e) {
//                e.printStackTrace();
//            }
//            oldBeginDate.before(newBeginDate);
//            if (oldBeginDate.before(newBeginDate)==true && oldEndDate.before(newEndDate)==true && oldBeginDate.before(newEndDate)==true && oldEndDate.before(newBeginDate)==true){
//                System.out.println("添加");
//                arr[i] = "0";
//            }else {
//                System.out.println("会议时间冲突");
//                arr[i] = "1";
//
//            }
//        }
//        String str = Arrays.toString(arr);
//        if (str.contains("1")){
//            return false;
//        }else {
////            meetingRoomManage.setNewdatetime(queueDate);
////            meetingRoomManage.setSubscribetime(queueDate);
//            meetingRoomManage.setUpdatetime(queueDate);
//            meetingRoomManageMapper.updateMeetingRoomManage(meetingRoomManage);
//            return true;
//        }


        Date queueDate = new Date();
        MeetingRoomManage meetingRoomManage1 = new MeetingRoomManage();
        meetingRoomManage1.setMrPlace(meetingRoomManage.getMrPlace());
        meetingRoomManage1.setMrmId(meetingRoomManage.getMrmId());
        List<MeetingRoomManage> meetingRoomManages = meetingRoomManageMapper.selectMeetingRoomManageList(meetingRoomManage1);

        //对比时间
        //开始时间
        Date begintime = meetingRoomManage.getBegintime();
        long begintime_ms = begintime.getTime();
        //系统时间
        long queueDateTime = queueDate.getTime();
        //结束时间
        Date finishtime = meetingRoomManage.getFinishtime();
        long finishtime_ms = finishtime.getTime();
        String[] arr = new String[meetingRoomManages.size()];
        boolean am_i = DateUtils.isSameDay(begintime, finishtime);
        if (!am_i) {
            BooleanUtils booleanUtils = new BooleanUtils();
            booleanUtils.setFlag(false);
            booleanUtils.setMag("开始时间和结束时间必须是同一天!");
            return booleanUtils;
        } else if (begintime_ms > finishtime_ms) {
            BooleanUtils booleanUtils = new BooleanUtils();
            booleanUtils.setFlag(false);
            booleanUtils.setMag("会议开始时间不能小于结束时间!");
            return booleanUtils;
        } else if (begintime_ms < queueDateTime) {
            BooleanUtils booleanUtils = new BooleanUtils();
            booleanUtils.setFlag(false);
            booleanUtils.setMag("会议开始时间不能小于当前时间时间!");
            return booleanUtils;
        }

        for (int i = 0; i < arr.length; i++) {
            MeetingRoomManage manage = meetingRoomManages.get(i);

            Date oldBeginTime = manage.getBegintime();
            Date oldFinishTime = manage.getFinishtime();
            Date newBeginTime = meetingRoomManage.getBegintime();
            Date newFinishTime = meetingRoomManage.getFinishtime();

            long oldBeginDate = oldBeginTime.getTime();
            long oldEndDate = oldFinishTime.getTime();
            long newBeginDate = newBeginTime.getTime();
            long newEndDate = newFinishTime.getTime();

            if ((oldBeginDate < newBeginDate && oldEndDate < newBeginDate) || (newBeginDate < oldBeginDate && newEndDate < oldBeginDate)) {
                System.out.println("修改");
                arr[i] = "0";
            } else {
                System.out.println("会议时间冲突");
                arr[i] = "1";
            }
        }
        String str = Arrays.toString(arr);
        if (str.contains("1")) {
            BooleanUtils booleanUtils = new BooleanUtils();
            booleanUtils.setFlag(false);
            booleanUtils.setMag("会议时间冲突");
            return booleanUtils;
        } else {
            meetingRoomManage.setUpdatetime(queueDate);
            meetingRoomManageMapper.updateMeetingRoomManage(meetingRoomManage);
            BooleanUtils booleanUtils = new BooleanUtils();
            booleanUtils.setFlag(true);
            booleanUtils.setMag(null);
            return booleanUtils;
        }

    }

    /**
     * 批量删除会议管理
     *
     * @param mrmIds 需要删除的会议管理主键
     * @return 结果
     */
    @Override
    public Boolean deleteMeetingRoomManageByMrmIds(Integer[] mrmIds) {
        String[] arr = new String[mrmIds.length];
        for (int i = 0; i < mrmIds.length; i++) {
            MeetingRoomManage manage = meetingRoomManageMapper.selectMeetingRoomManageByMrmId(mrmIds[i]);
            Date finishtime = manage.getFinishtime();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = formatter.format(finishtime);

            Date queueDate = new Date();
            SimpleDateFormat queueDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String now2 = queueDateFormat.format(queueDate);


            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date beginDate = null;
            Date endDate = null;
            try {
                beginDate = df.parse(dateString);
                endDate = df.parse(now2);
            } catch (ParseException e) {
                e.printStackTrace();
            }


            beginDate.before(endDate);
            if (beginDate.before(endDate) == true) {
                System.out.println("删除");
                arr[i] = "0";
            } else {
                System.out.println("未到时间");
                arr[i] = "1";

            }
        }

        String str = Arrays.toString(arr);
        if (str.contains("1")) {
            return false;
        } else {
            meetingRoomManageMapper.deleteMeetingRoomManageByMrmIds(mrmIds);
            return true;
        }
    }

    /**
     * 批量删除会议记录
     *
     * @param mrmIds 需要删除的会议管理主键
     * @return 结果
     */
    @Override
    public int deleteMeetingRoomManageRecordByMrmIds(Integer[] mrmIds) {
        return meetingRoomManageMapper.deleteMeetingRoomManageRecordByMrmIds(mrmIds);
    }


    /**
     * 删除会议管理信息
     *
     * @param mrmId 会议管理主键
     * @return 结果
     */
    @Override
    public int deleteMeetingRoomManageByMrmId(Integer mrmId) {
        MeetingRoomManage manage = meetingRoomManageMapper.selectMeetingRoomManageByMrmId(mrmId);
        Date finishtime = manage.getFinishtime();
        Date queueDate = new Date();
        SimpleDateFormat queueDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        String now2 = queueDateFormat.format(queueDate);

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = null;
        try {
            date1 = df.parse(String.valueOf(finishtime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date date2 = null;
        try {
            date2 = df.parse(now2);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        long long1 = date1.getTime();
        long long2 = date2.getTime();
        if (long1 > long2) {
            System.out.println("date1 > date2");
            System.out.println("删除");
        } else {
            System.out.println("时间没到");
        }
        return meetingRoomManageMapper.deleteMeetingRoomManageByMrmId(mrmId);
    }

    @Override
    public void rollbackEmployee(Object[] array) {
        meetingRoomManageMapper.rollbackEmployee(array);
    }

    @Override
    public Boolean removeSubscribe(Integer mrmId) {
        return meetingRoomManageMapper.removeSubscribe(mrmId);
    }
}
